home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Please help me
- Date: 26 Mar 1996 15:48:53 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4j93l5$f1g@sparcserver.lrz-muenchen.de>
- References: <4j6nrl$lfk@badger.wmin.ac.uk>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- darec@westminster.ac.uk (Nadarajah Thavaneethan) writes:
-
- >#include <stdio.h>
- >#include <stdlib.h>
- >#define SIZE 60
-
- >void main(void)
-
- I will not comment on this.
-
- >{
- > char c[SIZE];
- >int index, next;
- > for (index=0; index(SIZE && ( c[index] = getchar() != '\n'); index++);
-
- Is there a good reason not to use fgets()?
-
- > c[index] = 0;
- > for(; index > 0; index--)
- > for (next = 0; next < index; next++)
- > compchar(c[next-1],c[next]);
-
- It's probably not such a good idea to access "c[-1]". There are at least
- two reasons for this:
-
- 1) Pointer arithmetic is not defined on memory that does not belong to
- _one_ object.
-
- 2) "c[-1]" might refer to memory that does not belong to your program.
-
- > printf("\n the line is now %s\n",c);
- > }
-
- > void compchar (char c1, char c2)
- > {
-
- > char p;
-
-
- > if (c1 > c2)
- > p = c1;
- > c1 = c2;
- > c2 = p;
- > }
-
- C has _no_ call by reference. You have to use pointers to variables
- if you want to mimic "call by reference" in C. Since passing a
- pointer is what most programming languages that have call by reference
- do to implement it, this is _not_ a disadvantage, it just makes it
- more obvious to the programmer what is going on.
-
- Indentation does not express structure in C, so, as far as your
- compiler is concerned, only the first assignment depends on the
- "if".
-
- So:
-
- void compchar(char *c1, char *c2)
- {
- char p;
-
- if (*c1 > *c2) {
- p = *c1;
- *c1 = *c2;
- *c2 = p;
- }
- }
-
- might be the conditional swap you are looking for.
-
- >This program reads a line of data and sorts it into ascending ASCII sequence
- > it doesn't but it should.
-
- Obviously, all the program should do is either print "42" or guess what
- you really wanted it to do and do exactly that, depending on the phase
- of the moon and the current price for pickeled herring in the netherlands.
- The FAQ for comp.lang.c tells you why.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-
-